﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo.Framework.DataStructure.ListFlattening
{
    class ComplexLinkedList
    {
        private Node _head;

        public ComplexLinkedList()
        {
            _head = null;
        }

        public bool IsEmpty()
        {
            return _head == null;
        }

        public bool Insert(int position, int level, object data)
        {
            try
            {
                if (position < 0) { throw new Exception("Invalid Position"); }
                if(level<0) {throw new Exception("Invalid Level");}
                if(IsEmpty() && position != 0) {throw new Exception("Invalid Position");}

                // go to the correct level


                Node newNode = new Node(data);

                if (IsEmpty())
                {
                    _head = newNode;
                }
                else
                {
                    if (position == 0)
                    {
                        newNode.Next = _head;
                        _head = newNode;
                    }
                    else
                    {
                        Node currentNode = _head;
                        for (int i = 0; i < (position -1) && currentNode !=null ; i++)
                        {
                            currentNode = currentNode.Next;
                        }
                        if (currentNode == null) { throw new Exception("Invalid position"); }

                        newNode.Next = currentNode.Next == null ? null : currentNode.Next;                                                                        
                        currentNode.Next = newNode;
                        return true;                       
                    }
                }
                
                return true;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public object Remove(int position)
        {
            try
            {
                if (IsEmpty()) { throw new Exception("The list is already empty"); }
                if (position < 0) { throw new Exception("Invalid Position"); }

                Node deletedItem = null;

                if (position == 0)
                {
                    _head = _head.Next;
                }
                else
                {
                    Node currentNode = _head;
                    for (int i = 0; i < (position -1) && currentNode !=null; i++)
                    {
                        currentNode = currentNode.Next;
                    }
                    if (currentNode.Next == null) { throw new Exception("Invalid Position"); }

                    deletedItem = currentNode.Next;
                    currentNode.Next = currentNode.Next.Next == null ? null : currentNode.Next;                    
                }

                return deletedItem.Data;
            }
            catch (Exception ex)
            {
                
                throw;
            }
        }
    }
}
